package org.jeecg.modules.procedure.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.procedure.entity.Proc;
import org.jeecg.modules.procedure.entity.ProcVo;
import org.jeecg.modules.procedure.service.IProcService;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 工序表
 * @Author: jeecg-boot
 * @Date:   2020-07-16
 * @Version: V1.0
 */
@Api(tags="工序表")
@RestController
@RequestMapping("/procedure/proc")
@Slf4j
public class ProcController extends JeecgController<Proc, IProcService> {
	@Autowired
	private IProcService procService;

	/**
	 * 分页列表查询
	 *
	 * @param proc
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "工序表-分页列表查询")
	@ApiOperation(value="工序表-分页列表查询", notes="工序表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(Proc proc,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		LambdaQueryWrapper<Proc> queryWrapper = new LambdaQueryWrapper<>();
		if (proc.getCode() != null && !proc.getCode().equals("")){
			queryWrapper.like(Proc::getCode,proc.getCode());
		}
		if (proc.getName() != null && !proc.getName().equals("")){
			queryWrapper.like(Proc::getName,proc.getName());
		}
		queryWrapper.orderByAsc(Proc::getCreateTime);
		Page<Proc> page = new Page<Proc>(pageNo, pageSize);
		IPage<Proc> pageList = procService.page(page, queryWrapper);
		return Result.ok(pageList);
	}

	 @AutoLog(value = "工序表-分页列表查询")
	 @ApiOperation(value="工序表-分页列表查询", notes="工序表-分页列表查询")
	 @GetMapping(value = "/phoneList")
	 public Result<?> queryPagePhoneList(String procName,String userName,String isAll) {
		 List<Proc> procList = procService.getProcByUserName(procName,userName,isAll);
		 return Result.ok(procList);
	 }

	 @GetMapping(value = "/findProcByframe")
	 public Result<?> findProcByframe(String frameId,String userName) {
		 List<ProcVo> procList = procService.findProcByframe(frameId,userName);
		 return Result.ok(procList);
	 }


	 /**
	 *   添加
	 *
	 * @param proc
	 * @return
	 */
	@AutoLog(value = "工序表-添加")
	@ApiOperation(value="工序表-添加", notes="工序表-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody Proc proc) {
		procService.save(proc);
		return Result.ok("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param proc
	 * @return
	 */
	@AutoLog(value = "工序表-编辑")
	@ApiOperation(value="工序表-编辑", notes="工序表-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody Proc proc) {
		procService.updateById(proc);
		return Result.ok("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "工序表-通过id删除")
	@ApiOperation(value="工序表-通过id删除", notes="工序表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		procService.removeById(id);
		return Result.ok("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "工序表-批量删除")
	@ApiOperation(value="工序表-批量删除", notes="工序表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.procService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "工序表-通过id查询")
	@ApiOperation(value="工序表-通过id查询", notes="工序表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		Proc proc = procService.getById(id);
		if(proc==null) {
			return Result.error("未找到对应数据");
		}
		return Result.ok(proc);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param proc
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, Proc proc) {
        return super.exportXls(request, proc, Proc.class, "工序表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, Proc.class);
    }

}
